package models

import (
	"fmt"
	//"time"

	"github.com/jinzhu/gorm"
)

type PlanCase struct {
	gorm.Model
	PlanID                   string         `gorm:"column:planid"`
	PlanProduct              string         `gorm:"column:planproduct"`
	PlanEnv                  string         `gorm:"column:planenv"`
	PlanType                 string         `gorm:"column:plantype"`
	PlanModel                 string         `gorm:"column:planmodel"`
	PlanStatus                bool		     `gorm:"column:planstatus"`
	PlanCaseServerity         string		 `gorm:"column:plancaseserverity"`
	PlanPassCase              uint16        `gorm:"column:planpasscase"`
	PlanFailCase              uint16         `gorm:"column:planfailcase"`
	PlanOmitCase              uint16         `gorm:"column:planomitcase"`
	PlanTotalCase             uint16         `gorm:"column:plantotalcase"`
    PlanResult                bool		  `gorm:"column:planresult"`
	PlanResultUrl     		  string       `gorm:"column:planresulturl"`
	PlanReceiver              string         `gorm:"column:planreceiver"`
}

func (PlanCase) TableName() string {
	return "plan_case"
}

func (*PlanCase) Create(planid string, planproduct string, plantype string, planenv string, plancaseserverity string,  planmodel string, planreceiver string, planresulturl string) {
	plan := PlanCase{}
	plan.PlanID = planid
	plan.PlanProduct = planproduct
	plan.PlanType = plantype
	plan.PlanEnv = planenv
	plan.PlanCaseServerity = plancaseserverity
	plan.PlanModel = planmodel
	plan.PlanStatus = false
	plan.PlanReceiver = planreceiver	
	plan.PlanResultUrl = planresulturl
	db.Create(&plan)
}


func (*PlanCase) Update(planid string, plan PlanCase) {
	//fmt.Printf("修改%v的状态", plan)
	plannew := PlanCase{}
	db.Model(&plannew).Where("planid=?",planid).Updates(&plan)
}


func (*PlanCase) UpdateStatus(planid string) {
	plan := PlanCase{}
	plan.PlanID = planid
	//fmt.Printf("修改%v的状态", plan)
	db.Model(&plan).Where("planid=?",planid).Update("PlanStatus", true)
}

func (*PlanCase) Get(planid string) PlanCase {
	plan := PlanCase{}
	//fmt.Println("开始查找:", planid)
	db.Where("planid=?",planid).First(&plan)
	//fmt.Printf("plan:%+v", plan)
	return plan

}

func (*PlanCase) GetList() []PlanCase {
	plans := []PlanCase{}
	db.Order("planid desc").Limit(20).Find(&plans)
	//fmt.Printf("plan:%+v", plans)
	return plans

}

func (*PlanCase) Delete(PlanID string) {
	plan := PlanCase{}
	fmt.Println("delete recode:", PlanID)
	db.Where("planid=?",PlanID).Delete(plan)	
}

func (*PlanCase)GetExectTime(planid string) float64 {
	plan := PlanCase{}
	//fmt.Println("开始查找:", planid)
	db.Where("planid=?",planid).First(&plan)
	starttime := plan.CreatedAt
	endtime := plan.UpdatedAt
	execttime := endtime.Sub(starttime).Seconds()	
	return execttime
}